/*
 * Sender.java
 */

package org.sunspotworld.demo;
import com.sun.spot.io.j2me.radiogram.RadiogramConnection;
import com.sun.spot.util.Queue;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;

/**
 *
 * @author kkmitch
 * @revised by: mi zhang
 */
public class Sender {

    private boolean running = false;
    private RadiogramConnection txConn = null;
    private Datagram xdg = null;
    private int myPort = -1;
    private String myBaseStationAddress = null;

    public Sender(int TxPORT, String baseADDR) {

        myPort = TxPORT;
        myBaseStationAddress = baseADDR;
        System.out.println("Sender: initializing.");
        try {
            txConn = (RadiogramConnection) Connector.open("radiogram://" + myBaseStationAddress + ":" + Integer.toString(myPort));
            xdg = txConn.newDatagram(txConn.getMaximumLength());
        } catch (Exception ex) {
            System.err.println("Sender: Caught " + ex + " in txConn connection initialization in FR's Sender");
            ex.printStackTrace();
        }
        running = true;
    }

    // We do not stop sender
    public void stopRun(){
        System.out.println("Sender: stopRun().");
        // ????????????? Synchronization
        running = false;
        try {
            txConn.close();
        } catch (IOException ex) {
            System.err.println("Sender: Caught " + ex + " while closing the connection.");
            ex.printStackTrace();
        }
    }

    public void immediateSend(){

    }

    /*
     * Keep sending datagrams inside the sender queue.
     */
    public void senderRun(Queue sendQu){
        int debug_counter = 0;
        System.out.println("Sender: senderRun().");
        // ????????????? Synchronization
        while(true){
            if (running) {
                debug_counter++;
                System.out.println("Loop_Sender: " + debug_counter + " starts.");
                try{
                    xdg.reset();
                    xdg.writeUTF((String)sendQu.get()); // Blocking!
                    txConn.send(xdg);
                } catch (IOException ex) {
                    System.err.println("Sender: Caught " + ex + " while collecting/sending sensor samples.");
                    ex.printStackTrace();
                }
            } else
                break;
        }
    }

//    public void printReply(String reply) {
//        System.out.println(reply);
//    }
//
//    public void printFormat(Vector vt){
//        for(int i = 0 ; i < vt.size(); i++) {
//            System.out.print(vt.elementAt(i) + " ");
//        }
//        System.out.println("");
//    }
//
//    public void printCmd(byte[] bt) {
//        for(int i = 0; i < bt.length; i++) {
//            System.out.print(bt[i] + " ");
//        }
//        System.out.println("");
//    }
//
//    public void printData(Vector vt) {
//        int[] intArray;
//        for(int i = 0; i < vt.size(); i++) {
//            intArray = (int[]) vt.elementAt(i);
//            for(int j = 0; j < intArray.length; j++) {
//                System.out.println(intArray[j] + " ");
//            }
//            System.out.println("");
//        }
//    }
}
